/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: SystemHandler
 * @Package com.rx.uua.handler
 * @Description: 关于系统管理的处理
 * @author: 陈锦韬
 * @date: 2021\7\7 0007
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.stang.handler;

import com.rx.core.base.RxBaseData;
import com.rx.core.base.RxResponse;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.handler.RxHandler;
import com.rx.core.handler.RxHandlerPrev;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxContentHolder;
import com.rx.stang.service.proxy.STOutProxyService;
import com.rx.stang.vo.PermisionRequest;
import com.rx.stang.vo.TangColumnistInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Administrator
 * @Description: 关于系统管理的处理
 * @date: 2021\7\7 0007
 */
@RxHandler
@Slf4j
public class SuiTangHandler {
    public static final String  RANDOM_PASSWORD="RANDOM_PASSWORD";
    public static final String  NEWEST="1";
    public static final String  OLDE="0";
    public static final String  AUTH_KEY="AUTH_KEY";
    @Autowired
    StandardService standardService;

    @Autowired
    STOutProxyService outProxyService;

    List<PermisionRequest> hasAuthority(){
        PermisionRequest permisionRequest = new PermisionRequest();
        permisionRequest.setUserBid(RxContentHolder.getContext().getUserInfo().getUserInfo().getBid());
        List<PermisionRequest> list = outProxyService.permisionRequest(permisionRequest);
        return list;
    }

    boolean judgeNoAuthority(List<PermisionRequest> list,String title){
        if(EmptyChecker.isEmpty(list)){
            return true;
        }
        boolean hasAuth = false;
        for (PermisionRequest permisionRequest : list) {
            if(permisionRequest.getTitle().equals(title)){
                hasAuth = true;
                break;
            }
        }
        if(!hasAuth){
            return true;
        }
        return false;
    }


    @RxHandlerPrev(types= {HANDLER_TYPE.SINGLE_PAGE},keys = {
            "/rx-standard-api/single/tang-columnist-info/standard/page"
    })
    public boolean searchColumnistPage(RxBaseData data){

        Map<String,Object> condition = data.getCondition();
        if(condition == null){
            condition = new HashMap<>();
        }

        Boolean needCheck = (Boolean)data.getTmpVar(AUTH_KEY);
        if(needCheck == null || needCheck){
            List<PermisionRequest> list = hasAuthority();
            if(EmptyChecker.isEmpty(list)){
                data.setResultMessage("用户无权限查询");
                return true;
            }
            List<String> titleList = new ArrayList<>();
            for (PermisionRequest permisionRequest : list) {
                titleList.add(permisionRequest.getTitle());
            }
            condition.put("inTitle",titleList);
        }
        //  根据时间倒叙。
        condition.put("sqlOrderbydesc","id");

        // 返回字段 不过滤
        List filterList = new ArrayList();
        filterList.add("updateDate");
        data.removeFilter(filterList);

        data.setCondition(condition);
        return false;
    }

    public boolean shelvesColumnist(List<String> titleList,String from,String to){
        try {
            TangColumnistInfo condition =(TangColumnistInfo) RxBaseData.create(TangColumnistInfo.class,HANDLER_TYPE.SINGLE_LIST);
            condition.setIsNewest(from);

            Map<String,Object> map = new HashMap<>();
            map.put("inTitle",titleList);
            condition.setCondition(map);
            Object object = standardService.searchList(condition);
            if(object instanceof RxResponse){
                // 不用下架
                return true;
            }
            List<TangColumnistInfo> resultList = (List<TangColumnistInfo>)object;
            for (TangColumnistInfo info : resultList) {
                info.setIsNewest(to);
            }
            condition.setDataList((List)object);
            standardService.batchUpdate(condition);
            return true;
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        return false;
    }

    @RxHandlerPrev(types= {HANDLER_TYPE.SINGLE_INSERT},keys = {
            "/rx-standard-api/single/tang-columnist-info/standard/insert"
    })
    public boolean obtainAuthForInsert(RxBaseData data){
        TangColumnistInfo tangColumnistInfo = (TangColumnistInfo)data;
        if(EmptyChecker.isEmpty(tangColumnistInfo.getContent())){
            data.setResultMessage("内容为空");
            return true;
        }
        List<PermisionRequest> list = hasAuthority();
        if(judgeNoAuthority(list,tangColumnistInfo.getTitle())){
            data.setResultMessage("用户无权限操作" + tangColumnistInfo.getTitle());
            return true;
        }
        tangColumnistInfo.setAuthor(RxContentHolder.getContext().getUserInfo().getUserInfo().getRealName());
        tangColumnistInfo.setIsNewest(NEWEST);
        List<String> listColumnist = new ArrayList<>();
        listColumnist.add(tangColumnistInfo.getTitle());
        if(!shelvesColumnist(listColumnist,NEWEST,OLDE)){
            data.setResultMessage("原寄语下架失败！");
            return true;
        }

        return false;
    }

    @RxHandlerPrev(types= {HANDLER_TYPE.SINGLE_BATCH_SAVE},keys = {
            "/rx-standard-api/single/tang-columnist-info/standard/batch-save"
    })
    public boolean obtainAuth(RxBaseData data){
        log.debug(data.toString());

        if(EmptyChecker.isEmpty(data.getDataList())){
            data.setResultMessage("输入为数据为空");
            return true;
        }
        List<PermisionRequest> list = hasAuthority();

        // 是否又权限修改。
        for (RxBaseData rxBaseData : data.getDataList()) {
            TangColumnistInfo tangColumnistInfo = (TangColumnistInfo)rxBaseData;
            // 作者
            tangColumnistInfo.setAuthor(RxContentHolder.getContext().getUserInfo().getUserInfo().getRealName());
            tangColumnistInfo.setIsNewest(NEWEST);

            if(judgeNoAuthority(list,tangColumnistInfo.getTitle())){
                data.setResultMessage("用户无权限修改" + tangColumnistInfo.getTitle());
                return true;
            }
        }

        // 获取
        return false;
    }
}
